package com.zzj.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzj.entity.Post;
import com.zzj.mapper.PostMapper;
import com.zzj.model.dto.post.PostCreateDto;
import com.zzj.model.dto.post.PostUpdateDto;
import com.zzj.model.dto.QueryListDto;
import com.zzj.service.IPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzj.utils.QueryUtil;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author zzj
 * @since 2023-07-17
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {
    @Override
    public IPage<Post> queryList(QueryListDto in) {
        QueryWrapper<Post> queryWrapper = QueryUtil.paramsWrapper(in.getAndParams(), in.getOrParams(), in.getOrderParams());
        Page<Post> postPage = new Page<>(in.getPageNo(), in.getPageSize());
        return this.getBaseMapper().selectPage(postPage, queryWrapper);
    }

    @Override
    public Post create(PostCreateDto in) {
        Post post = JSONObject.parseObject(JSON.toJSONString(in), Post.class);
        this.save(post);
        return post;
    }

    @Override
    public Post update(PostUpdateDto in) {
        Post post = JSONObject.parseObject(JSON.toJSONString(in), Post.class);
        this.updateById(post);
        return post;
    }
}
